<template>
  <div class="adress_list">
    <group>
      <cell title="新增收货地址" is-link class="add_btn" link="/add_adress"></cell>
    </group>
    <div class="adress_item" v-for="item in addlist">
      <cell :title="item.userName" :value="item.userPhone"></cell>
      <cell primary="content" :value="item.userAddress"></cell>
      <check-icon :value.sync="item.isDefault" @click.native="setdefault(item)"> 设为默认</check-icon>
      <div class="adres_btn edit_btn">编辑</div>
      <div class="adres_btn del_btn" @click="del_address(item)">删除</div>
    </div>
    <p v-if="addlist==''" class="list_none">列表为空~</p>
    <toast v-model="success_dialog" type="text" text="操作成功"></toast>
  </div>
</template>
<script>
import g from "../../common/js/http.js"
import { Cell, Group, CheckIcon } from "vux"
export default {
  components: {
    Group,
    Cell,
    CheckIcon
  },
  data() {
    return {
      demo1: false,
      addlist: [],
      success_dialog: false,
      list_null: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData() {
      var _this = this
      g.post_func("/api/v1/staff/adress_list", {}, function(data) {
        _this.addlist = data.data
        _this.addlist.forEach(v => {
          if (v.isDefault == 0) {
            v.isDefault = false
          } else {
            v.isDefault = true
          }
        })
      })
    },
    setdefault(item) {
      var _this = this
      var req_data = {
        adress_id: item.addressId,
        userId: item.userId
      }
      g.post_func("/api/v1/setAdressDefault", req_data, function(data) {
        if (data.code == 200) {
          _this.getData()
        }
      })
    },
    del_address(item) {
      var _this = this
      if(item.isDefault == 1){
        alert('默认地址不能删除')
        return;
      }
      g.post_func(
        "/api/v1/staff/del_adress",
        { addressId: item.addressId },
        function(data) {
          if (data.code == 200) {
            _this.success_dialog = true
            _this.getData()
          }
        }
      )
    }
  }
}
</script>
<style scoped>
.adress_list {
  font-size: 96%;
}
.edit_btn {
  background-image: url("../../../static/icon/edit_icon.png");
  right: 70px !important;
}
.del_btn {
  background-image: url("../../../static/icon/del_icon.png");
}
.adres_btn {
  background-size: 20px;
  background-repeat: no-repeat;
  background-position: 2px 3px;
  padding: 5px 0 5px 25px;
  position: absolute;
  right: 10px;
  bottom: 12px;
}
.add_btn {
  position: relative;
  background-image: url("../../../static/icon/add.png");
  background-size: 25px;
  background-repeat: no-repeat;
  background-position: 8px 8px;
  padding-left: 36px;
}
.adress_item {
  position: relative;
  padding: 15px 0px;
  background: #e1e1e1;
  margin: 10px 0;
}
.list_none {
  text-align: center;
  margin-top: 10%;
}
</style>